package com.invengo.base.utils;

import static com.invengo.library_db.entity.module_check.BookState.ERROR_NO_LISTING;
import static com.invengo.library_db.entity.module_check.BookState.ERROR_OTHER_SHELF;
import static com.invengo.library_db.entity.module_check.BookState.IN_SHELF;

import android.annotation.SuppressLint;
import android.os.Build;

import androidx.annotation.RequiresApi;

import com.invengo.library_db.entity.module_check.BookBean;
import com.invengo.library_db.entity.module_check.ShelfBean;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import me.goldze.mvvmhabit.utils.cache.ACache;
import me.goldze.mvvmhabit.utils.cache.AKey;

public class BeanMapperUtils {
    static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 格式化时间


    public static List<BookBean> transFlowBookBean(List<BookBean> bookBeans, String currentShelfCode, String currentShelfName, boolean isParallel) {
        String dateTime = format.format(new Date());
        Integer StaffID = (Integer) ACache.getInstance().getAsObject(AKey.CACHE_USER_STAFF_ID);
        String userName = ACache.getInstance().getAsString(AKey.CACHE_USERNAME);
        for (BookBean bean : bookBeans) {
            if (!ACache.getInstance().getAsBoolean(AKey.IS_OFFLINE_MODEL)){
                if (!isParallel) {
                    bean.setOldShelfCode(bean.getShelfCode());
                    bean.setOldShelfName(bean.getShelfName());
                    bean.setShelfCode(currentShelfCode);
                    bean.setShelfName(currentShelfName);
                }
            }
            bean.setUserName(userName);
            bean.setUserId(StaffID);
            bean.setOpeationDate(dateTime);
//            if (!isParallel)
//                bean.setState(1);
            switch (bean.getTip()) {
                case ERROR_OTHER_SHELF:
                case ERROR_NO_LISTING:
                    bean.setNPDStatus(3);
                    break;
                case IN_SHELF:
                    bean.setNPDStatus(1);
                    break;
                default:
                    bean.setNPDStatus(0);
                    break;
            }
        }
        return bookBeans;
    }

    public static List<BookBean> cloneList(List<BookBean> bookBeans) {
        List<BookBean> beans = new ArrayList<>();
        for (BookBean bean : bookBeans) {
            try {
                beans.add(bean.clone());
            } catch (CloneNotSupportedException e) {
                throw new RuntimeException(e);
            }
        }
        return beans;
    }

    public static List<ShelfBean> cloneShelfList(List<ShelfBean> shelfBeans) {
        List<ShelfBean> beans = new ArrayList<>();
        shelfBeans.forEach(bean -> {
            try {
                beans.add(bean.clone());
            } catch (CloneNotSupportedException e) {
                throw new RuntimeException(e);
            }
        });
        return beans;
    }
}
